#import "ToDo.h"
/*
TODO LIST
*/
@implementation ToDo


- (NSComparisonResult)compareAlpha:(ToDo *)myToDo
{
    return [[NSString stringWithFormat:@"%@", 
                                       [self getDescription]]
            compare:
            [NSString stringWithFormat:@"%@", 
                                       [myToDo getDescription]]];
}
- (NSComparisonResult)comparePriority:(ToDo *)myToDo
{
    if([self getPriority] > [myToDo getPriority]) {
		return NO;
	}
	else {
		return YES;
	}
}
- (NSComparisonResult)comparePosition:(ToDo *)myToDo
{
    if([self getPosition] > [myToDo getPosition]) {
		return YES;
	}
	else {
		return NO;
	}
}
- (NSComparisonResult)compareDueDate:(ToDo *)myToDo
{
	NSCalendarDate *selfDate;
	NSCalendarDate *secondDate;
	
	selfDate = [[NSCalendarDate init] alloc];
	secondDate = [[NSCalendarDate init] alloc];
	selfDate = [self getDueDate];
	secondDate = [myToDo getDueDate];
	
	if(!selfDate) {
		selfDate = [[NSCalendarDate calendarDate] dateByAddingYears:1 months:0 days:0 hours:0 minutes:0 seconds:0];
	}
	if(!secondDate) {
		secondDate = [[NSCalendarDate calendarDate] dateByAddingYears:1 months:0 days:0 hours:0 minutes:0 seconds:0];
	}
    if([[selfDate earlierDate:secondDate] isEqual:selfDate]) {
		return NO;
	}
	else {
		return YES;
	}
}
- (NSComparisonResult)compareCreationDate:(ToDo *)myToDo
{
	NSCalendarDate *selfDate;
	NSCalendarDate *secondDate;
	
	selfDate = [[NSCalendarDate init] alloc];
	secondDate = [[NSCalendarDate init] alloc];
	selfDate = [self getCreationDate];
	secondDate = [myToDo getCreationDate];
	
	if(!selfDate) {
		selfDate = [[NSCalendarDate calendarDate] dateByAddingYears:1 months:0 days:0 hours:0 minutes:0 seconds:0];
	}
	if(!secondDate) {
		secondDate = [[NSCalendarDate calendarDate] dateByAddingYears:1 months:0 days:0 hours:0 minutes:0 seconds:0];
	}
    if([[selfDate earlierDate:secondDate] isEqual:selfDate]) {
		return NO;
	}
	else {
		return YES;
	}
}
- (id)init
{
	if (!self) self = [super init];

	[self setDescription:@"--"];
	return self;
}

- (NSString *)getDescription
{
	return description;
}

- (void)setDescription:(NSString *)myDescription
{
	myDescription = [myDescription copy];
	[description release];
	description = myDescription;
}

- (void)setSource:(NSString *)mySource
{
	mySource = [mySource copy];
	[source release];
	source = mySource;
}
- (NSString *)getSource
{
	return source;
}

- (void)setGroup:(int)groupid
{
	group = groupid;
}
- (int)getGroup
{
	return group;
}
- (void)setDueDate:(NSCalendarDate *)date
{
	date = [date copy];
	[dueDate release];
	dueDate = date;
}
- (NSCalendarDate *)getDueDate
{
	return dueDate;
}
- (void)setCreationDate:(NSCalendarDate *)date
{
	date = [date copy];
	[creationDate release];
	creationDate = date;
}
- (NSCalendarDate *)getCreationDate
{
	return creationDate;
}

- (void)setCheckedDate:(NSCalendarDate *)date
{
	date = [date copy];
	[checkedDate release];
	checkedDate = date;
}
- (void)setCheckedNow
{
	NSCalendarDate *myDate = [NSCalendarDate calendarDate];
	myDate = [myDate copy];
	[checkedDate release];
	checkedDate = myDate;
}
- (void)setUnchecked
{
	checkedDate = nil;
}
- (NSCalendarDate *)getCheckedDate
{
	return checkedDate;
}

- (void)setPriority:(int)prio
{
	priority = prio;
}
- (int)getPriority
{
	return priority;
}

- (void)setDone:(int)state
{
	done = state;
}
- (int)getDone
{
	return done;
}

- (void)setPosition:(int)pos
{
	position = pos;
}
- (int)getPosition
{
	return position;
}

- (void)setIcon:(int)myIcon
{
	icon = myIcon;
}
- (int)getIcon
{
	return icon;
}

- (BOOL)isExpired
{
	
	if(![[dueDate earlierDate:[NSCalendarDate calendarDate]] isEqual:dueDate]) {
		return NO;
	}
	return YES;
}

- (ToDo *)copy
{
	ToDo *newToDo = [[ToDo alloc] init];
	[newToDo setDescription:@""];
	[newToDo setCreationDate:[NSCalendarDate calendarDate]];
	[newToDo setDueDate:[[NSCalendarDate calendarDate] dateByAddingYears:0 months:0 days:5 hours:0 minutes:0 seconds:0]];
	[newToDo setGroup:0];
	[newToDo setPriority:0];
	[newToDo setPosition:-1];
	[newToDo setDone:NO];
	return newToDo;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
	[coder encodeObject:description forKey:@"description"];
	[coder encodeObject:source forKey:@"source"];
	[coder encodeInt:done forKey:@"done"];
	[coder encodeInt:priority forKey:@"priority"];
	[coder encodeInt:group forKey:@"group"];
	[coder encodeInt:position forKey:@"position"];
	[coder encodeInt:icon forKey:@"icon"];
	// creationDate2 for compatibility with older versions!
	[coder encodeObject:creationDate forKey:@"creationDate2"];
	[coder encodeObject:dueDate forKey:@"dueDate"];
	[coder encodeObject:checkedDate forKey:@"checkedDate"];
}

- (id)initWithCoder:(NSCoder *)coder
{
	self = [super init];
	
	[self setDescription:[coder decodeObjectForKey:@"description"]];
	[self setSource:[coder decodeObjectForKey:@"source"]];
	[self setDone:[coder decodeIntForKey:@"done"]];
	[self setPosition:[coder decodeIntForKey:@"position"]];
	[self setPriority:[coder decodeIntForKey:@"priority"]];
	// creationDate2 for compatibility with older versions!
	[self setCreationDate:[coder decodeObjectForKey:@"creationDate2"]];
	[self setDueDate:[coder decodeObjectForKey:@"dueDate"]];
	[self setCheckedDate:[coder decodeObjectForKey:@"checkedDate"]];
	[self setGroup:[coder decodeIntForKey:@"group"]];
	[self setIcon:[coder decodeIntForKey:@"icon"]];
	
	return self;
}

- (void)dealloc
{
	[description release];
	[self setDone:nil];
	[super dealloc];
}

@end